package com.rzgeo.rzmaplib.comp;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.Checkable;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.rzgeo.rzmaplib.R;


/**
 * Created by zhanghang on 2017/10/19.
 */

public class RmlCheckableRelativeLayout extends RelativeLayout implements Checkable {

    private boolean isChecked = false;
    private int _normalBk = Integer.MIN_VALUE;
    private int _checkedColor= Integer.MIN_VALUE;

    public RmlCheckableRelativeLayout(Context context) {
        super(context);
        initUI(context);
    }

    public RmlCheckableRelativeLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initUI(context);
    }

    public RmlCheckableRelativeLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initUI(context);
    }

    private void initUI(Context context) {

        if(_normalBk == Integer.MIN_VALUE||_checkedColor== Integer.MIN_VALUE){
            if(getBackground() instanceof ColorDrawable){
                ColorDrawable drawable=(ColorDrawable)getBackground();
                _normalBk =drawable.getColor();
            }else{

            }
            _checkedColor=context.getResources().getColor(R.color.rwlBkPrimarySelect);
        }
//        this.setBackgroundColor(_normalBk);
    }


    @Override
    public void setChecked(boolean checked) {
        isChecked = checked;
        changeColor(checked);
    }

    @Override
    public boolean isChecked() {
        return false;
    }

    @Override
    public void toggle() {
        this.isChecked = !this.isChecked;
        changeColor(this.isChecked);
    }


    private void changeColor(boolean isChecked) {
        //根据check的状态切换颜色
        if (isChecked) {
            setBackgroundColor(_checkedColor);
        } else {
            setBackgroundColor(_normalBk);
        }
    }
}
